﻿Imports System.Data.SqlClient

Public Class Utilisateurs

    Private user As String
    Private userSite As String
    Private cn As CnData
    Private da As SqlDataAdapter
    Private ds As DataSet
    Private cmd As SqlCommand
    Private dtr As SqlDataReader

    Public Sub New(ByVal user As String, ByRef cn As CnData, ByVal userSite As String)
        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        Me.user = user
        Me.cn = cn
        Me.userSite = userSite

    End Sub

    Private Sub Utilisateurs_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        da = New SqlDataAdapter("SELECT * FROM Sites WHERE Actif = 1", cn.getCn)
        ds = New DataSet
        da.Fill(ds, "SitesU")
        With SiteU
            .DataSource = ds.Tables("SitesU")
            .DisplayMember = "NomDoc"
            .ValueMember = "NumSite"
            .SelectedValue = userSite
        End With
        cn.Open()
        da = New SqlDataAdapter("SELECT Nom,Prenom,Util as Login,Profil,Responsable FROM Utilisateurs WHERE Profil <> 'Admin' AND Site = '" & userSite & "' AND UtilS is null", cn.getCn)
        da.Fill(ds, "ListeU")
        Liste.DataSource = ds.Tables("ListeU")
        cn.Close()
    End Sub

    Private Sub Liste_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles Liste.RowHeaderMouseDoubleClick
        If e.RowIndex < 0 Then Exit Sub
        If MsgBox("Voulez vous supprimer compte utilisateur '" & Liste.Item(2, e.RowIndex).Value.ToString & "' ?", MsgBoxStyle.YesNo) = vbYes Then
            cn.Open()
            cmd = New SqlCommand("UPDATE Utilisateurs SET DateS='" & Date.Today & "', UtilS='" & user & "' WHERE Util='" & Liste.Item(2, e.RowIndex).Value.ToString & "' AND DateS IS NULL", cn.getCn())
            cmd.ExecuteNonQuery()
            Try
                ds.Tables("ListeU").Clear()
            Catch ex As Exception

            End Try

            da = New SqlDataAdapter("SELECT Nom,Prenom,Util as Login,Profil,Responsable FROM Utilisateurs WHERE Profil <> 'Admin' AND Site = '" & SiteU.SelectedValue & "' AND UtilS is null", cn.getCn)
            da.Fill(ds, "ListeU")
            'Liste.DataSource = ds.Tables("ListeU")

            cn.Close()
        End If
    End Sub

    Private Sub SiteU_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SiteU.SelectedIndexChanged
        cn.Open()
        If String.IsNullOrEmpty(SiteU.ValueMember) Then Exit Sub
        Try
            ds.Tables("ListeU").Clear()
        Catch ex As Exception

        End Try

        da = New SqlDataAdapter("SELECT Nom,Prenom,Util as Login,Profil,Responsable FROM Utilisateurs WHERE Profil <> 'Admin' AND Site = '" & SiteU.SelectedValue & "' AND UtilS is null", cn.getCn)
        da.Fill(ds, "ListeU")
        cn.Close()
    End Sub

    Private Sub nouveau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nouveau.Click
        Dim ChildForm As New NouveauUtil(user, cn, SiteU.SelectedValue, "Création", "")
        If ChildForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            cn.Open()
            ds.Tables("ListeU").Clear()
            da = New SqlDataAdapter("SELECT Nom,Prenom,Util as Login,Profil,Responsable FROM Utilisateurs WHERE Profil <> 'Admin' AND Site = '" & SiteU.SelectedValue & "' AND UtilS is null", cn.getCn)
            da.Fill(ds, "ListeU")
            cn.Close()
        End If
    End Sub

    Private Sub Liste_CellContentDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Liste.CellDoubleClick
        If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Exit Sub

        Dim ChildForm As New NouveauUtil(user, cn, SiteU.SelectedValue, "Modification", Liste.Item(2, e.RowIndex).Value.ToString)
        If ChildForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            cn.Open()
            ds.Tables("ListeU").Clear()
            da = New SqlDataAdapter("SELECT Nom,Prenom,Util as Login,Profil,Responsable FROM Utilisateurs WHERE Profil <> 'Admin' AND Site = '" & SiteU.SelectedValue & "' AND UtilS is null", cn.getCn)
            da.Fill(ds, "ListeU")
            cn.Close()
        End If
    End Sub

    Private Sub Fermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fermer.Click
        Me.Close()
    End Sub
End Class